package com.example.demo3.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsbj.common.msg.DataPageMessage;
import com.dsbj.common.msg.ResponseMessage;
import com.example.demo3.entity.ResourceDevice;
import com.example.demo3.service.ResourceDeviceService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/devicewcm")
public class DeviceWcmController extends BaseController{

    @Resource
    private ResourceDeviceService resourceDeviceService;

    @RequestMapping("/deviceHtml")
    public String deviceHtml(){
        return "deviceEdit/deviceHtml";
    }

    @RequestMapping("/deviceData")
    @ResponseBody
    public DataPageMessage deviceData(int pageindex,int pagesize,int sEcho,String resourceDeviceNo,String resourceDeviceName){
       /* List<ResourceDevice> list = resourceDeviceService.getList();
        return DataPageMessage.ok(list.subList((pageindex-1)*pagesize,Math.min(pageindex*pagesize,list.size())),list.size(),list.size(),sEcho,"");*/
        Page<ResourceDevice> resourceDevicePage =resourceDeviceService.getSelectPage(resourceDeviceNo,resourceDeviceName,pageindex,pagesize);
        if(resourceDevicePage==null||resourceDevicePage.getRecords()==null){
            return DataPageMessage.ok(null,(int)resourceDevicePage.getCurrent(),(int)resourceDevicePage.getCurrent(),sEcho,"");
        }
        return DataPageMessage.ok(resourceDevicePage.getRecords(),(int)resourceDevicePage.getTotal(),(int)resourceDevicePage.getTotal(),sEcho,"");
    }

    @RequestMapping("/edit")
    public String edit(HttpServletRequest request, Model model, String id){
        ResourceDevice resourceDevice = resourceDeviceService.get(id);
        if(null != resourceDevice){
            model.addAttribute("resourceDevice",resourceDevice);
        }
        return "deviceEdit/edit";
    }

    @RequestMapping("/update")
    @ResponseBody
    public ResponseMessage update(ResourceDevice resourceDevice){
        resourceDevice.setUpdateUser("wcm");
        resourceDevice.setUpdateTime(new Date());
        resourceDeviceService.update(resourceDevice);
        return ResponseMessage.ok("修改成功!");
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseMessage delete(String id){
        resourceDeviceService.delete(id);
        return ResponseMessage.ok("删除成功!");
    }

    @RequestMapping("/delBatch")
    @ResponseBody
    public ResponseMessage delBatch(String ids){
        resourceDeviceService.delBatch(ids);
        return ResponseMessage.ok("删除成功!");
    }

    @RequestMapping("/add")
    public String add(){
        return "deviceEdit/add";
    }

    @RequestMapping("/insert")
    @ResponseBody
    public ResponseMessage insert(ResourceDevice resourceDevice){
        resourceDevice.setCreateUser("wcm");
        resourceDevice.setStatus(0);
        resourceDevice.setDeleteStatus(0);
        resourceDeviceService.insert(resourceDevice);
        return ResponseMessage.ok("新增成功!");
    }

}
